<?php
	class Penempatan_kelasController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Akademik";
		public $subMenu = "";
		public $title = "";
		public $loginUser; 
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("akademik")){
				LoginUser::redirectToLoginPage("akademik");
			}else{
				$this->loginUser = LoginUser::getUser();
			}			
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_siswa_pnk");
			$this->subMenu = "Penempatan Kelas";
			$data=array();
			$arrDepartment = MDepartment::model()->findAll("is_active = 1");
			$department_id = "";
			if ($arrDepartment != null){
				$department_id = $arrDepartment[0]->id;
			}
			
			$arrTahunAjaran = MTahunAjaran::model()->findAll("is_active = 1 and department_id = :departmentId",array(":departmentId"=>$department_id));
			$activeTahunAjaran = CMTahunAjaran::getTahunAjaranActive($department_id);
			$tahun_ajaran_id = "";
			
			if ($activeTahunAjaran != null){
				$tahun_ajaran_id = $activeTahunAjaran->id;
			}
			
			$data["arrDepartment"] = $arrDepartment;
			$data["arrTahunAjaran"] = $arrTahunAjaran;
			$data["department_id"] = $department_id;
			$data["tahun_ajaran_id"] = $tahun_ajaran_id;
				
			$this->layout ="//layouts/ly_blank";			
			$this->render('index',$data);
		}
		
		public function actionListNoKelas(){
			$department_id = Yii::app()->request->getParam("department_id","1");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			$conn = Yii::app()->db;
			try{				
				$strFields="a.id,a.department_id,b.name as tahun_ajaran,sid,a.name,if(gender='P','Perempuan','Laki-laki') as gender,birth_date";								
				$sql = "select $strFields from m_student a inner join m_tahun_ajaran b on a.tahun_ajaran_id = b.id where a.id not in 
					(select student_id from m_student_class b inner join v_kelas_tahun_ajaran c on b.kelas_tahun_ajaran_id = c.id where c.tahun_ajaran_id = :tahunAjaranId and b.is_active = true ) 
					and a.department_id =:deptId and a.is_active = true and a.status_siswa ='ACTIVE'";				
				$sql .= " order by id";								

				$params = array(
						"deptId"=>$department_id,
						"tahunAjaranId"=>$tahun_ajaran_id,
				);
				
				$command = $conn->createCommand($sql);		
				$rs =$command->query($params);
				$rw = $rs->readAll();
				$this->result["actionStatus"] =0;
				$this->result["actionMessage"] ="";
				$this->result["actionError"] =array();
				$this->result["model"] = $rw;
			}catch(Exception $e){
				$this->result["actionStatus"] =2;
				$this->result["actionMessage"] =$e->getMessage();
			}			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionListKelas(){
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$conn = Yii::app()->db;
			try{
				$sql = "select a.*,b.sid as sid ,b.name as student_name,if(gender='P','Perempuan','Laki-laki') as gender from m_student_class a inner join m_student b on a.student_id = b.id  where a.kelas_tahun_ajaran_id = :kelasTahunAjaranId and a.is_active = true";
				$params = array(
						":kelasTahunAjaranId"=>$kelas_tahun_ajaran_id,
					);
		
				$command = $conn->createCommand($sql);
				$rs =$command->query($params);
				$rw = $rs->readAll();
				
				$this->result["actionStatus"] =0;
				$this->result["actionMessage"] ="";
				$this->result["actionError"] =array();
				$this->result["model"] = $rw;
			}catch(Exception $e){
				$this->result["actionStatus"] =2;
						$this->result["actionMessage"] =$e->getMessage();
						}
			header('Content-type:application/json');
				echo CJSON::encode($this->result);
		}
					
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
		public function actionedit(){
			$op = Yii::app()->request->getParam("op","add");
			if ($op=="add"){
				if (!LoginUser::getPermission("akademik_siswa_pnk_add",null,false)){
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "Anda tidak memiliki ijin untuk menambah data!");
					$this->returnJSON($this->result);
				}				
				$studentId = Yii::app()->request->getParam("student_id","");
				$kelasTahunAjaranId = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
				try{
					if ($studentId =="" or $kelasTahunAjaranId ==""){
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "Siswa atau kelas tidak valid ");
					}else{
						$kelasTahunAjaran = MKelasTahunAjaran::model()->findByPk($kelasTahunAjaranId);
						$sql = "select * from m_student_class a inner join v_kelas_tahun_ajaran b on a.kelas_tahun_ajaran_id = b.id
							where tahun_ajaran_id = :tahunAjaranId and student_id =:studentId and a.is_active= true";
						
						$conn = Yii::app()->db;
						$cmd = $conn->createCommand($sql);						
						$params = array(":tahunAjaranId"=>$kelasTahunAjaran->tahun_ajaran_id,":studentId"=>$studentId);
						$rs = $cmd->query($params);
						if ($rs->count() > 0){
							//Sudah terdaftar di kelas untuk tahun ajaran ini
							$rw =$rs->read();
							$this->result["actionStatus"] = 1;
							$this->result["actionMessage"] = Yii::t("dialog", "Siswa telah terdaftar di kelas "). $rw["kelas_name"];
						}else{
							//belum terdaftar kelas manapun untuk tahun ajaran ini
							$kelasTahunAjaran = MKelasTahunAjaran::model()->findByPk($kelasTahunAjaranId);
							
							if ($kelasTahunAjaran == null){
								$this->result["actionStatus"] = 1;
								$this->result["actionMessage"] = Yii::t("dialog", "kelas tidak valid ");
							}else{
								
								$studentClass = MStudentClass::model()->find("student_id =:studentId and kelas_tahun_ajaran_id =:kelasTahunAjaranId",
										array(":studentId"=>$studentId,":kelasTahunAjaranId" =>$kelasTahunAjaranId));
								if ($studentClass == null){ 
									$studentClass = new MStudentClass();
									$studentClass->student_id = $studentId;
									$studentClass->kelas_tahun_ajaran_id = $kelasTahunAjaranId;
									$studentClass->input_date = date("Y-m-d H:i:s");
									$studentClass->monthly_fee = $kelasTahunAjaran->monthly_fee;
								}else{
									$studentClass->last_update_date = date("Y-m-d H:i:s");
								}								
								$studentClass->is_active = 1;
								$studentClass->last_user_id = $this->loginUser["id"];
								
								if ($studentClass->save()){
									$this->result["actionStatus"] = 0;
									$this->result["id"] = $studentClass->id;
									
								}else{
									$this->result["actionStatus"] = 2;
									$this->result["errorMessage"] = $studentClass->getErrors();
								}
							}
						}
					}
				}catch (Exception $e){
					$this->result["actionStatus"] = 2;
					$this->result["errorMessage"] = $e->getMessage();
				}
				$this->returnJSON($this->result);
			}else if($op=="del"){
				if(!LoginUser::getPermission("akademik_siswa_pnk_del",null,false)){
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "Anda tidak memiliki ijin untuk menghapus data!");
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				
				try{
					$studentClassId = Yii::app()->request->getParam("student_class_id","");					
					if ($studentClassId ==""){
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "Id tidak valid ");
					}else{
						$studentClass = MStudentClass::model()->findByPk($studentClassId);
						$studentClass->is_active = 0;
						$studentClass->delete_date=Date("Y-m-d H:i:s");
						$studentClass->last_user_id = $this->loginUser["id"];
						if ($studentClass->save()){
							$this->result["actionStatus"] = 0;
							$this->result["id"] = $studentClass->student_id;
						}else{
							$this->result["actionStatus"] = 2;
							$this->result["errorMessage"] = $studentClass->getErrors();
						}
					}
				}catch (Exception $e){
					$this->result["actionStatus"] = 2;
					$this->result["actionMessage"] = $e->getMessage();
				}
				$this->returnJSON($this->result);
			}
		}
		
		public function actiongetWebcam(){
			$data =array("callback"=>Yii::app()->request->getParam("callback",""));
			$this->layout ="//layouts/ly_blank";
			$this->render('webcam',$data);
		}
		
		public function actionClassHist(){			
			$studentId = Yii::app()->request->getParam("student_id","");			
			$this->subMenu = "Riwayat Kelas";
			$this->layout ="//layouts/ly_blank";
			$data=array("student_id"=>$studentId);
			$this->render('classHist',$data);
		}
		public function actionListClassHist(){
			$studentId = Yii::app()->request->getParam("student_id","");
			$conn = Yii::app()->db;
			if ($studentId != ""){
				$sql = "select a.id,a.student_id,b.tahun_ajaran_name,b.tingkatan_name,b.kelas_name from m_student_class a inner join v_kelas_tahun_ajaran b on a.kelas_tahun_ajaran_id = b.id where a.is_active = true and student_id = :studentId";
				$cmd = $conn->createCommand($sql);
				$rs = $cmd->query(array("studentId"=>$studentId));
				$this->result["actionStatus"] = 0;
				$this->result["actionMessage"] = "";
				$this->result["model"] = $rs->readAll();
			}else{
				$this->result["actionStatus"] = 1;
				$this->result["model"] = array();				
			}
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			
		}
	}
?>